package cn.tour.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.tour.pojo.Platform_User;
import cn.tour.service.UserService;

@Controller
public class UserController {
	private Logger logger=Logger.getLogger(UserController.class);
	@Resource
	private UserService userService;
	//跳转到注册页面
	@RequestMapping(value="/useradd",method=RequestMethod.GET)
	private String userAdd(@ModelAttribute("user")Platform_User user) {
		return "register";
	}
	//处理注册
	@RequestMapping(value="/useraddsave",method=RequestMethod.POST)
	public String userAddSave(Platform_User user) {
		logger.info("userAddSave------->user:"+user);
		if(userService.userAdd(user)) {
			return "login";
		}
		return "register";
	}
	//异步验证手机号的唯一性
	@RequestMapping(value="phoneexist.json")
	@ResponseBody
	public Object getPhone(@RequestParam("user_Phone")String user_Phone) {
		logger.info("getPhone-------->user_Phone:"+user_Phone);
		HashMap<String,String> resultMap=new HashMap<String,String>();
		if(StringUtils.isNullOrEmpty(user_Phone)) {
			resultMap.put("Phone", "empty");
		}else {
			Platform_User user=userService.getPhone(user_Phone);
			if(user!=null) {
				resultMap.put("Phone", "exist");
			}else {
				resultMap.put("Phone", "noexist");
			}
		}
		return JSONArray.toJSONString(resultMap);
	}
}
